package com.swpu.cbg.cnshop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.cjj.MaterialRefreshLayout;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.swpu.cbg.cnshop.Constans;
import com.swpu.cbg.cnshop.R;
import com.swpu.cbg.cnshop.adapter.BaseAdapter;
import com.swpu.cbg.cnshop.adapter.DividerItemDecortion;
import com.swpu.cbg.cnshop.adapter.ShopDetilAdapter;
import com.swpu.cbg.cnshop.bean.PageContent;
import com.swpu.cbg.cnshop.bean.Ware;
import com.swpu.cbg.cnshop.http.Pager;
import com.swpu.cbg.cnshop.utils.CartProvider;
import com.swpu.cbg.cnshop.widget.cnToolbar;

import java.util.List;

public class ShopDetailActivity extends BaseActivity implements Pager.OnPagerListener<Ware>, TabLayout.OnTabSelectedListener, View.OnClickListener {

    @ViewInject(R.id.id_shop_list_tab)
    private TabLayout mLayout;
    @ViewInject(R.id.toolbar)
    private cnToolbar mToolBar;
    @ViewInject(R.id.id_shop_list_tv)
    private TextView mTitleView;
    @ViewInject(R.id.id_shop_list_refresh)
    private MaterialRefreshLayout materialRefreshLayout;
    @ViewInject(R.id.id_shop_list_recyclerview)
    private RecyclerView mRecyclerView;

    //活动id
    private int campaignId = 1;

    //     默认： 0   销售： 1  价格：  2
    private static final int TAG_NORMAL = 0;
    private static final int TAG_SALE = 1;
    private static final int TAG_PRICE = 2;
    private int orderBy = TAG_NORMAL;

    private static final int ACTION_LIST = 0;
    private static final int ACTION_GRID = 1;

    private ShopDetilAdapter mAdapter;
    private Pager mPager;
    private CartProvider mProvider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_detail);
        ViewUtils.inject(this);
        campaignId = Integer.parseInt(String.valueOf(getIntent().getLongExtra(Constans.API.CAMPAIGN_ID, 0)));
        initTab();
        initView();
        initToolBar();
    }

    private void initToolBar() {
        mToolBar.hideRightButton();
        mToolBar.showEditButton();
        showList();
        mToolBar.getmEditButton().setOnClickListener(this);

    }

    private void showGrid() {
        mToolBar.getmEditButton().setText("列表");
        mToolBar.getmEditButton().setTag(ACTION_GRID);
    }


    private void showList() {
        mToolBar.getmEditButton().setText("网格");
        mToolBar.getmEditButton().setTag(ACTION_LIST);
    }

    ;

    private void initView() {
        mPager = Pager.newBuilder()
                .setLoadMore(true)
                .setmMaterialRefreshLayout(materialRefreshLayout)
                .setUrl(Constans.API.CAMPAIGN_URL)
                .setOnPagerListener(this)
                .putParamter("campaignId", campaignId)
                .putParamter("orderBy", orderBy)
                .builder(this, new TypeToken<PageContent<Ware>>() {
                }.getType());
        mPager.request();
    }

    private void initTab() {

        TabLayout.Tab DefaultTab = mLayout.newTab();
        DefaultTab.setText("默认");
        DefaultTab.setTag(TAG_NORMAL);

        TabLayout.Tab SaleTab = mLayout.newTab();
        SaleTab.setText("销量");
        SaleTab.setTag(TAG_SALE);

        TabLayout.Tab PriceTab = mLayout.newTab();
        PriceTab.setText("价格");
        PriceTab.setTag(TAG_PRICE);

        mLayout.addTab(DefaultTab);
        mLayout.addTab(SaleTab);
        mLayout.addTab(PriceTab);

        mLayout.setOnTabSelectedListener(this);
    }

    @Override
    public void load(List<Ware> data, int totlePage, int totleCount) {
        mTitleView.setText("共有" + totleCount + "件商品");
        if (mAdapter == null) {
            mAdapter = new ShopDetilAdapter(this, data, R.layout.template_hot_recycleview);
            mAdapter.setOnItemClickListener(new BaseAdapter.onItemClickListener() {
                @Override
                public void onClick(View view, int position) {
                    Intent intent = new Intent();
                    intent.setClass(ShopDetailActivity.this, WareDetilActivity.class);
                    intent.putExtra(Constans.API.TAG_WARE, mAdapter.getDatas().get(position));
                    startActivity(intent);
                }
            });
            mAdapter.setOnItemButtonClickListener(new ShopDetilAdapter.OnItemButtonClickListener() {
                @Override
                public void OnClick(View view, Ware ware) {
                    mProvider = new CartProvider(ShopDetailActivity.this);
                    mProvider.put(ware);
                    Toast.makeText(ShopDetailActivity.this, "添加" + ware.getName() + "成功", Toast.LENGTH_SHORT).show();
                }
            });
            mRecyclerView.setAdapter(mAdapter);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            mRecyclerView.addItemDecoration(new DividerItemDecortion());
            mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        } else {
            mAdapter.refreshData(data);
        }

    }

    @Override
    public void loadMore(List<Ware> data, int totlePage, int totleCount) {
        mAdapter.loadMoreData(data);
    }

    @Override
    public void refresh(List<Ware> data, int totlePage, int totleCount) {
        mAdapter.refreshData(data);
        mRecyclerView.scrollToPosition(0);
    }


    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        orderBy = (int) tab.getTag();
        mPager.putParamter("orderBy", orderBy);
        mPager.request();
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void onClick(View v) {
        int tag = (int) v.getTag();
        switch (tag) {
            case ACTION_GRID:
                showList();
                mAdapter.resetLayout(R.layout.template_hot_recycleview);
                mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
                mRecyclerView.addItemDecoration(new DividerItemDecortion());
                mRecyclerView.setItemAnimator(new DefaultItemAnimator());
                break;

            case ACTION_LIST:
                showGrid();
                mAdapter.resetLayout(R.layout.template_shop_gridview);
                mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
                mRecyclerView.addItemDecoration(new DividerItemDecortion());
                mRecyclerView.setItemAnimator(new DefaultItemAnimator());
                break;
        }
    }
}
